<?php
	class SearchController extends Controller{
		
		
		/**
		 * 显示搜索页面
		 */
		public function index(){
			$words = isset($_POST['words'])?htmlspecialchars($_POST['words']):'';
			$model = D('ask');
			/**
			 * 搜索相关关键字的问题
			 */
			 //返回搜索的结果集
			$result = $model->query("SELECT asid, content, time, solve, answer, title FROM hd_ask JOIN hd_category ON hd_ask.cid = hd_category.cid WHERE content LIKE '%{$words}%' ORDER BY time DESC");
			$this->assign('searchResult',$result);
			$this->assign('words',$words);
			$this->display();
		}
		
		
		
		/**
		 * ajax异步搜索功能
		 */
		public function ajax_search(){
			$keyword = strip_tags($_GET['key']);
			$ask = D('ask');
			$keywords = $ask->query("SELECT * FROM hd_ask WHERE content LIKE '%{$keyword}%' LIMIT 5");
			if(count($keywords)==0){
				echo 0;
			}else{
				echo json_encode($keywords, JSON_UNESCAPED_UNICODE);	
			}
		}
		
		
	}
